const http = require('../../../utils/http.js');
Page({
  data: {
    collegeList: [],
    majorList: {},
    currentCollegeId: null // 当前展开的学院ID
  },

  onLoad() {
    this.getCollegeList();
  },

  getCollegeList() {
    http.get('/vocationalcollege/list').then(res =>{
      console.log(res);
      this.setData({
        collegeList: res.data.data
      });
    //   this.getMajorsForColleges(this.data.collegeList);
    }).catch(err=>{
      console.log(err);
      wx.showToast({
        title: '系统错误，请稍后重试',
        icon: Error
      })
    })
  },

  getMajorsForColleges(colleges) {
    colleges.forEach(college => {
      const postData =  { collegeId: college.id }
      http.get('/vocationalmajor/listByCollegeId' , postData).then(res =>{
        // console.log(res);
        const majorList = this.data.majorList;
            majorList[college.id] = res.data.data;
            this.setData({
              majorList: majorList
            });
      }).catch(err=>{
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: Error
        })
      })
    });
  },
  getMajorsByCollgeId(collegeId){
    const postData =  { collegeId: collegeId }
    http.get('/volunteermajor/listByCollegeId' , postData).then(res =>{
        // console.log(res);
        const majorList = this.data.majorList;
          majorList[collegeId] = res.data.data;
          this.setData({
            majorList: majorList
          });
      }).catch(err=>{
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: Error
        })
      })
  },

  // 新增方法，用于处理学院项的点击事件
  toggleMajorList(e) {
    const collegeId = e.currentTarget.dataset.id;
    // console.log("collegeId:"+collegeId)
    // 设置目前的点击院校的id
    this.setData({
      currentCollegeId: this.data.currentCollegeId === collegeId ? null : collegeId
    });
    // 根据院校id获取专业列表
    if(!this.data.majorList[collegeId]){
        this.getMajorsByCollgeId(collegeId);
    }
  }
});